package com.example.sfm.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.TeachersDto;
import com.example.sfm.pojo.dto.TeachersImportDto;
import com.example.sfm.pojo.dto.TeachersQueryDto;
import com.example.sfm.result.Result;
import com.example.sfm.service.ITeachersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@RestController("adminTeachersController")
@RequestMapping("/admin/teachers")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "教师管理接口", description = "提供教师信息的增删改查、导入导出等操作")
public class TeachersController {
    private final ITeachersService teachersService;

    /**
     * 创建教师档案
     */
    @PostMapping("/create")
    @Operation(summary = "创建教师档案", description = "根据传入的教师信息创建新的教师档案")
    public Result<?> createTeacher(@RequestBody @Validated @Parameter(description = "教师信息") TeachersDto dto) {
        log.info("创建教师档案: {}", dto);
        teachersService.createTeacher(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 分页查询教师
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询教师", description = "根据查询条件分页查询教师信息")
    public Result<?> listTeachers(@ParameterObject TeachersQueryDto dto) {
        log.info("查询教师列表: {}", dto);
        return Result.success(teachersService.listTeachers(dto), MessageConstants.SELECT_OK);
    }

    /**
     * 更新教师信息
     */
    @PutMapping("/update/{id}")
    @Operation(summary = "更新教师信息", description = "根据教师ID更新教师的相关信息")
    public Result<?> updateTeacher(@Parameter(description = "教师ID",in = ParameterIn.PATH) @PathVariable Integer id, @RequestBody @Validated TeachersDto dto) {
        log.info("更新教师信息: ID={}", id);
        teachersService.updateTeacher(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除教师
     */
    @DeleteMapping("/delete")
    @Operation(summary = "批量删除教师", description = "根据传入的教师ID列表批量删除教师信息")
    public Result<?> deleteTeachers(@Parameter(description = "教师ID列表",in = ParameterIn.QUERY) @RequestParam List<Integer> ids) {
        log.info("批量删除教师: IDs={}", ids);
        teachersService.deleteTeachers(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @PostMapping("/import")
    @Operation(summary = "导入教师信息", description = "通过上传Excel文件导入教师信息")
    public Result<?> importExcel(@Parameter(description = "Excel文件") @RequestParam MultipartFile file) {
        log.info("导入教师");
        teachersService.importTeachers(file);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    @GetMapping("/export")
    @Operation(summary = "导出教师信息", description = "根据查询条件导出教师信息到Excel文件")
    public void exportExcel(HttpServletResponse response,@ParameterObject TeachersQueryDto dto) {
        log.info("导出教师");
        teachersService.exportTeachers(response, dto);
    }

    /**
     * 下载教师导入模板
     */
    @GetMapping("/template")
    @Operation(summary = "下载教师导入模板", description = "下载用于导入教师信息的Excel模板文件")
    public void downloadTemplate(@NotNull HttpServletResponse response) throws IOException {
        // 设置响应头
        String fileName = URLEncoder.encode(String.valueOf(LocalDate.now()), StandardCharsets.UTF_8);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        List<TeachersImportDto> data = Collections.emptyList();

        // 使用 EasyExcel 写入 Excel 文件
        EasyExcel.write(response.getOutputStream(), TeachersImportDto.class)
                .sheet("教师信息")
                .doWrite(data);
    }
}